package cn.huq.day03

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}


object AggregateByKeyDemo {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("FlatMapValuesDemo").setMaster("local")
    val sc = new SparkContext(conf)

    val arr: Array[(String, Int)] = Array(
      ("scala", 5), ("hadoop", 8),("scala", 2), ("hadoop", 3),
      ("hadoop", 1),("flink", 2), ("scala", 5),("scala", 2), ("hadoop", 3))
    val rdd: RDD[(String, Int)] = sc.parallelize(arr, 2)


//    val rdd2: RDD[(String, Int)] = rdd.aggregateByKey(0)(_ + _, _ + _)
    val rdd2: RDD[(String, Int)] = rdd.foldByKey(100)(_ + _)
//    val rdd2: RDD[(String, Int)] = rdd.aggregateByKey(100)(Math.max(_, _), _ + _)
    
    println(rdd2.collect().toBuffer)


  }

}
